package com.lemurian.bankdata.action.base;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.lemurian.bankdata.lite.authentication.BankDataPrincipal;
import com.lemurian.bankdata.lite.logger.BankDataLogger;
import com.lemurian.bankdata.lite.struts.Pager;
import com.lemurian.bankdata.lite.struts.PartialList;
import com.lemurian.bankdata.lite.utils.FormatData;
import com.lemurian.bankdata.lite.utils.GeneralConstant;
import com.lemurian.bankdata.services.ActivityLogService;
import com.lemurian.bankdata.utils.BaseAction;
import com.lemurian.entity.ActivityLog;
import com.lemurian.entity.User;
import com.opensymphony.xwork2.Preparable;

public class ActivityLogAction extends BaseAction implements Preparable {

	private static final long serialVersionUID = 1L;
	
	private BankDataLogger log = BankDataLogger.getLogger(ActivityLogAction.class.getSimpleName());

	private User user;
	private BankDataPrincipal principal;
	
	private String startTime="";
    private String endTime="";
    private String userInActivity="";
	
	private ActivityLog activityLog = new ActivityLog();
	private List<ActivityLog> activityLogs = new ArrayList<ActivityLog>();
	
	@Autowired private ActivityLogService activityLogService;
	
	@Override
	public void prepare() throws Exception {
		principal = (BankDataPrincipal) getSession().get(GeneralConstant.USER_PRINCIPAL);
		user = principal.getUser();
	}
	
	@Override
	public String list() {
		log.info("list user activity log");
		setPreviousPage(GeneralConstant.LIST);

        try {
            Criterion criterion = null;
            if(startTime == null && endTime !=null && startTime.length() > 0) {
            	endTime = endTime.trim();
                criterion = Restrictions.le("endTime", FormatData.datetimeFormatHM.parse(endTime));
                log.sInfo("Search criteria : endTime <= %s", endTime.toString());
            } else if(startTime != null && endTime ==null && endTime.length() > 0) {
            	startTime = startTime.trim();
                criterion = Restrictions.ge("startTime", FormatData.datetimeFormatHM.parse(startTime));
                log.sInfo("Search criteria : startTime >= %s", startTime.toString());
            } else if(startTime != null && endTime !=null && startTime.length() > 0 && endTime.length() > 0) {
            	endTime = endTime.trim();
            	startTime = startTime.trim();
                criterion = Restrictions.and(Restrictions.ge("startTime", FormatData.datetimeFormatHM.parse(startTime)), Restrictions.le("endTime", FormatData.datetimeFormatHM.parse(endTime)));
                log.sInfo("Search criteria : startTime >= %s", startTime.toString());
                log.sInfo("Search criteria : endTime <= %s", endTime.toString());
            } else {
                criterion = Restrictions.ne("id", new Integer(-1));
            }
            
            log.sInfo(" :: Fetch %s record(s) from index %s", getCount(), getStart());
            
            log.sInfo(" :: User      [%s] ", userInActivity);
            log.sInfo(" :: Criterion [%s] ", criterion);
            log.sInfo(" :: Start     [%s] ", getStart());
            log.sInfo(" :: Count     [%s] ", getCount());
            log.sInfo(" :: Order By  [%s] ", getOrderBy());
            log.sInfo(" :: Asc Desc  [%s] ", getAscDesc());
            
            PartialList<ActivityLog> partialList = activityLogService.findByCriteria(getStart(), getCount(),
            		getAscDesc().equalsIgnoreCase("asc") ? Order.desc(getOrderBy().length() > 0 ? getOrderBy() : "id") : Order.asc(getOrderBy().length() > 0 ? getOrderBy() : "id"),
            		userInActivity != null && userInActivity.length() > 0 ? Restrictions.ilike("loginId", "%" + userInActivity + "%") : Restrictions.ne("id", new Integer(-1)),
            		criterion);

            activityLogs = partialList.getList();

            setPager(Pager.generatePager(getStart(), getCount(), partialList.getTotal()));
            setPagerItem(Pager.generatePagerItem(getStart(), getCount(), partialList.getTotal()));
            
            partialList = null;
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        setMappedRequest(GeneralConstant.LIST);
        return SUCCESS;
	}
	
	@Override
	public String add() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String detail() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String edit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	public ActivityLog getActivityLog() {
		return activityLog;
	}

	public void setActivityLog(ActivityLog activityLog) {
		this.activityLog = activityLog;
	}

	public List<ActivityLog> getActivityLogs() {
		return activityLogs;
	}

	public void setActivityLogs(List<ActivityLog> activityLogs) {
		this.activityLogs = activityLogs;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getUserInActivity() {
		return userInActivity;
	}

	public void setUserInActivity(String userInActivity) {
		this.userInActivity = userInActivity;
	}
	
}